/**
 * Created by Myron on 2016/2/14.
 */
var async = require('async');
var config = require('../../../config.js'); //必要
var wxjsapi = require('wx_jsapi_sign'); //微信分享必要
var fs = require('fs');
var MyronWX=require('../../../lib/MyronWx.js');

var _VIEW_PATH = '../app_module/lanternfestival/views/';
var serviceM = require('../models/service.js');
var jumpurl= 'http://'+config.domain+'/extra';
module.exports = {
    index:function(req,res){
        console.log(111111111111);
        res.render(_VIEW_PATH+'lantern/index',{title:'正月十五'});
        //res.json(111111);
        //res.render(_VIEW_PATH+'lantern/index');
    },
    //活动主页面
    index1: function (req, res) {
        var openid = req.AV.user.get('openid');
        var uid = req.AV.user.id;
        var url = 'http://'+config.domain+'/extra'+req.url;

        var wxconfig={
            appId: config.wx.app_id,
            appSecret: config.wx.app_secret,
            cache_json_file:config.wx.cache_json_file
        };
        MyronWX.getUserInfo(wxconfig,openid).then(function(UserInfo){
            if(UserInfo.subscribe === 0){  //判断是否关注过
                console.log('没有关注，请先关注果然100');
                res.render(_VIEW_PATH+'lantern/err',{});
            }else{
                //读取微信分享KEY
                wxjsapi.getSignature(wxconfig)(url, function(error, result) {
                    if (error) {
                        res.json({
                            'error': error
                        });
                    } else {
                        //-------------正式的代码------------s------
                        serviceM.getHomeInfo(openid,uid).then(function(data){
                            console.log(data);
                            if(data.code === 0){
                                res.render(_VIEW_PATH+'lantern/index',{title:'正月十五闹元宵',data:data,appId:result.appId,timestamp:result.timestamp,nonceStr:result.nonceStr,signature:result.signature,url:jumpurl,nickname:UserInfo.nickname,headimgurl:UserInfo.headimgurl});
                            }else{
                                //err
                                console.log('没有找到用户');
                                res.render(_VIEW_PATH+'lantern/err');
                            }
                        }).catch(function(err){
                            //catch
                            console.log('报异常了');
                            console.log(err);
                            res.render(_VIEW_PATH+'lantern/err');
                        });
                        //-------------正式的代码------------e------
                    }
                });
            }
        });


    },

    //song
    giveCard: function (req, res) {
        var url = 'http://'+config.domain+'/extra'+req.url;
        var wxconfig={
            appId: config.wx.app_id,
            appSecret: config.wx.app_secret,
            cache_json_file:config.wx.cache_json_file
        };
        var openid=req.AV.user.get('openid');
        MyronWX.getUserInfo(wxconfig,openid).then(function(UserInfo){
            if(UserInfo.subscribe === 0){
                console.log('没有关注，请先关注果然100');
                res.render(_VIEW_PATH+'lantern/err',{});
            }else{
                //读取微信分享KEY
                wxjsapi.getSignature(wxconfig)(url, function(error, result) {
                    if (error) {
                        res.json({
                            'error': error
                        });
                    } else {
                        //获取成功分享页面
                        var reward_id = req.params.reward ;
                        var reward_name = req.params.reward_name ;
                        var uid = req.AV.user.id;
                        //创建一条赠送记录
                        serviceM.createCardFlow('lantern_give_reward',{ownuid:uid,reward:reward_id,number:-1,status:0,reward_name:reward_name}).then(function(data){
                            if(data.code === 0){
                                //res.render(_VIEW_PATH+'lantern/song',{title:'赠送给好友',reward:reward_id,user_name:user_name,user_img:user_img,card_id:data.card_id,reward_name:reward_name,appId:result.appId,timestamp:result.timestamp,nonceStr:result.nonceStr,signature:result.signature,url:url,nickname:UserInfo.nickname,headimgurl:UserInfo.headimgurl});
                                res.render(_VIEW_PATH+'lantern/song',{title:'赠送给好友',reward:reward_id,card_id:data.card_id,reward_name:reward_name,appId:result.appId,timestamp:result.timestamp,nonceStr:result.nonceStr,signature:result.signature,url:jumpurl,nickname:UserInfo.nickname,headimgurl:UserInfo.headimgurl});
                            }else{
                                //报错，执行刷新？返回？
                                res.render(_VIEW_PATH+'lantern/err')
                            }
                        }).catch(function(err){
                            res.render(_VIEW_PATH+'lantern/err')
                        });
                        //res.render(_VIEW_PATH+'lantern/song',{title:'赠送给好友',reward:'',user_name:'',user_img:'',card_id:'',appId:result.appId,timestamp:result.timestamp,nonceStr:result.nonceStr,signature:result.signature,url:url,nickname:UserInfo.nickname,headimgurl:UserInfo.headimgurl});
                    }
                });

            }
        });

    },
    fromsong: function (req, res) {
        var card_id = req.params.card_id ;
        //res.render(_VIEW_PATH+'lantern/fromsong',{title:'赠送好友',reward:"",reward_name:"",ownuid:"",card_id:card_id})
        serviceM.openGivePage(card_id).then(function(data){
            res.render(_VIEW_PATH+'lantern/fromsong',
                {title:'赠送好友',
                    data:data,
                    reward:data.reward,
                    reward_name:data.reward_name,
                    ownuid:data.ownuid,
                    card_id:card_id,
                    imgUrl:data.imgUrl,
                    nickname:data.nickname});
        }).catch(function(err){
            res.render(_VIEW_PATH+'lantern/err')
        });

    },
    //yao
    yaoCard: function (req, res) {
        var url = 'http://'+config.domain+'/extra'+req.url;
        var reward_id = req.params.reward ;
        var reward_name = req.params.reward_name ;
        var uid = req.AV.user.id;
        var wxconfig={
            appId: config.wx.app_id,
            appSecret: config.wx.app_secret,
            cache_json_file:config.wx.cache_json_file
        };
        var openid=req.AV.user.get('openid');
        MyronWX.getUserInfo(wxconfig,openid).then(function(UserInfo){
                if(UserInfo.subscribe === 0){
                    console.log('没有关注，请先关注果然100');
                    res.render(_VIEW_PATH+'lantern/err',{});
                }else{
                    //读取微信分享KEY
                    wxjsapi.getSignature(wxconfig)(url, function(error, result) {
                        if (error) {
                            res.json({
                                'error': error
                            });
                        } else {
                            //获取成功分享页面
                            serviceM.seekCardFlow('lantern_seek_reward',{ownuid:uid,reward:reward_id,number:1,status:0,reward_name:reward_name}).then(function(data){
                                if(data.code === 0){
                                    res.render(_VIEW_PATH+'lantern/yao',{title:'赠送给好友',ownuid:uid,reward:reward_id,card_id:data.card_id,reward_name:reward_name,appId:result.appId,timestamp:result.timestamp,nonceStr:result.nonceStr,signature:result.signature,url:jumpurl,nickname:UserInfo.nickname,headimgurl:UserInfo.headimgurl});

                                    //res.render(_VIEW_PATH+'lantern/song',{title:'赠送给好友',reward:reward_id,user_name:user_name,user_img:user_img,card_id:data.card_id,reward_name:reward_name});
                                }else{
                                    //报错，执行刷新？返回？
                                    res.render(_VIEW_PATH+'lantern/err')
                                }
                            }).catch(function(err){
                                res.render(_VIEW_PATH+'lantern/err')
                            });
                            //res.render(_VIEW_PATH+'lantern/yao',{appId:result.appId,timestamp:result.timestamp,nonceStr:result.nonceStr,signature:result.signature,url:url,nickname:UserInfo.nickname,headimgurl:UserInfo.headimgurl});
                        }
                    });

                }
        });
    },
    fromyao: function (req, res) {
        res.render(_VIEW_PATH+'lantern/fromyao',{});
    },
    test:function(req, res){
        res.render(_VIEW_PATH+'lantern/index',{});
    },

    giveReward:function(req,res,next){
        var uid = req.AV.user.id;
        serviceM.laoLanter(uid).then(function(data){
            res.json(data);
        }).catch(function(err){
            console.log('catch')
            console.log(err)
            res.json(err);
            res.send(502);
        });
    },
    giveCard1:function(req,res,next){
        var reward_id = req.params.reward ;
        var reward_name = req.params.reward_name ;
        var uid = req.AV.user.id;
        var user_name = req.AV.user.get('nickname');
        var user_img = req.AV.user.get('headimgurl');
        //创建一条赠送记录
        serviceM.createCardFlow('lantern_give_reward',{ownuid:uid,reward:reward_id,number:-1,status:0,reward_name:reward_name}).then(function(data){
            if(data.code === 0){
                //res.render(_VIEW_PATH+'lantern/song',{title:'赠送给好友',reward:reward_id,user_name:user_name,user_img:user_img,card_id:data.card_id,reward_name:reward_name,appId:result.appId,timestamp:result.timestamp,nonceStr:result.nonceStr,signature:result.signature,url:url,nickname:UserInfo.nickname,headimgurl:UserInfo.headimgurl});
                res.render(_VIEW_PATH+'lantern/song',{title:'赠送给好友',reward:reward_id,user_name:user_name,user_img:user_img,card_id:data.card_id,reward_name:reward_name,appId:'',timestamp:'',nonceStr:'',signature:'',url:'',nickname:'',headimgurl:''});
            }else{
                //报错，执行刷新？返回？
                res.render(_VIEW_PATH+'lantern/err')
            }
        }).catch(function(err){
            res.render(_VIEW_PATH+'lantern/err')
        });
    },
    yaoCard1:function(req,res,next){
        var reward_id = req.params.reward ;
        var reward_name = req.params.reward_name ;
        console.log(req.params.reward);
        var uid = req.AV.user.id;
        var user_name = req.AV.user.get('nickname');
        var user_img = req.AV.user.get('headimgurl');
        //创建一条赠送记录
        serviceM.seekCardFlow('lantern_seek_reward',{ownuid:uid,reward:reward_id,number:1,status:0,reward_name:reward_name}).then(function(data){
            if(data.code === 0){
                res.render(_VIEW_PATH+'lantern/yao',{title:'赠送给好友',reward:reward_id,user_name:user_name,user_img:user_img,card_id:data.card_id,reward_name:reward_name,appId:"",timestamp:"",nonceStr:"",signature:"",url:"",nickname:"",headimgurl:""});

                //res.render(_VIEW_PATH+'lantern/song',{title:'赠送给好友',reward:reward_id,user_name:user_name,user_img:user_img,card_id:data.card_id,reward_name:reward_name});
            }else{
                //报错，执行刷新？返回？
                res.render(_VIEW_PATH+'lantern/err')
            }
        }).catch(function(err){
            res.render(_VIEW_PATH+'lantern/err')
        });
    },
    fromyao_:function(req,res,next){
        var card_id = req.params.id;
        var uid = req.AV.user.id ;
        serviceM.openSeekPage(uid,card_id).then(function(data){
            console.log(data.card);
            res.render(_VIEW_PATH+'lantern/fromyao',
                {title:'向好友讨一个元宵',
                    ownuid:data.card.ownuid,
                    number:data.number,
                    reward_name:data.card.reward_name,
                    reward:data.card.reward,
                    card_id:card_id,
                    imgUrl:data.card.imgUrl,
                    nickname:data.card.nickname}
            );
        });
    },
    yao_get:function(req,res,next){
        var uid = req.AV.user.id ;
        var reward = req.query.reward ;
        var ownuid = req.query.ownuid ;
        var reward_name = req.query.reward_name ;
        //console.log('reward:'+reward);
        //console.log('ownuid:'+ownuid);
        //console.log('reward_name:'+reward_name);
        //console.log({uid:uid,ownuid:ownuid,reward:reward,reward_name:reward_name,number:1,status:1});
        serviceM.giveCardToFriend({uid:uid,ownuid:ownuid,reward:reward,reward_name:reward_name,number:1,status:1}).then(function(data){
            res.json(data);
            //res.render(_VIEW_PATH+'lantern/fromyao',{number:data.number,reward_name:data.card.reward_name,reward:data.card.reward,card_id:card_id});
        }).catch(function(err){
            res.json(err);
        });
    },
    song_get:function(req,res,next){
        var uid = req.AV.user.id ;
        var reward = req.query.reward ;
        var ownuid = req.query.ownuid ;
        var card_id = req.query.card_id ;
        var reward_name = req.query.reward_name ;
        serviceM.getCardFromFriend('lantern_give_reward',card_id,uid).then(function(data){
            res.json(data);
            //res.render(_VIEW_PATH+'lantern/fromyao',{number:data.number,reward_name:data.card.reward_name,reward:data.card.reward,card_id:card_id});
        }).catch(function(err){
            res.json(err);
        });
    },
    get3CardRandom:function(req,res,next){
        var uid = req.AV.user.id ;
        serviceM.get3CardRandom(uid).then(function(data){
            console.log(data)
            res.json(data);
            //res.render(_VIEW_PATH+'lantern/fromyao',{number:data.number,reward_name:data.card.reward_name,reward:data.card.reward,card_id:card_id});
        }).catch(function(err){
            res.json(err);
        });
    },
    shareCallback:function(req,res,next){

        var uid = req.AV.user.id ;
        var table = req.query.table ;
        var card_id = req.query.card_id ;
        var imgUrl = req.query.imgUrl;
        var nickname = req.query.nickname ;
        serviceM.shareCallback(table,card_id,uid,imgUrl,nickname).then(function(data){
            console.log(data)
            res.json(data);
            //res.render(_VIEW_PATH+'lantern/fromyao',{number:data.number,reward_name:data.card.reward_name,reward:data.card.reward,card_id:card_id});
        }).catch(function(err){
            res.json(err);
        });
    },
    yaoShareCallback:function(req,res,next){
        var uid = req.AV.user.id ;
        var table = req.query.table ;
        var card_id = req.query.card_id ;
        var imgUrl = req.query.imgUrl;
        var nickname = req.query.nickname ;
        serviceM.yaoShareCallback(table,card_id,uid,imgUrl,nickname).then(function(data){
            console.log(data)
            res.json(data);
            //res.render(_VIEW_PATH+'lantern/fromyao',{number:data.number,reward_name:data.card.reward_name,reward:data.card.reward,card_id:card_id});
        }).catch(function(err){
            res.json(err);
        });
    },
    getCurrentReward:function(req,res,next){
        serviceM.getCurrentReward().then(function(data){
            res.json(data);
        }).catch(function(err){
            res.json(err);
        });
    },

    //last give reward
    get_zsm_info:function(req,res,next){
        var uid = req.AV.user.id ;
        serviceM.get_zsm_info(uid).then(function(data){
            res.json(data);
            /*if(data.code === 0){
             res.json({code:0,msg:'操作成功，请前往商城个人中心查看'});
             }else{
             res.json({code:-1,msg:'操作失败，请刷新后重试，或者联系客服'});
             }*/
        }).catch(function(err){
            res.json(err);
            //res.json({code:9999,msg:'操作失败，请刷新后重试，或者联系客服'});
        });
    },
    give_apple:function(req,res,next){
        var uid = req.AV.user.id ;
        serviceM.give_apple(uid).then(function(data){
            res.json(data);
            /*if(data.code === 0){
             res.json({code:0,msg:'操作成功，请前往商城个人中心查看'});
             }else{
             res.json({code:-1,msg:'操作失败，请刷新后重试，或者联系客服'});
             }*/
        }).catch(function(err){
            res.json(err);
            //res.json({code:9999,msg:'操作失败，请刷新后重试，或者联系客服'});
        });
    },
    give_amount:function(req,res,next){
        var uid = req.AV.user.id ;
        serviceM.give_amount(uid).then(function(data){
            res.json(data);
            /*if(data.code === 0){
                res.json({code:0,msg:'操作成功，请前往商城个人中心查看'});
            }else{
                res.json({code:-1,msg:'操作失败，请刷新后重试，或者联系客服'});
            }*/
        }).catch(function(err){
            res.json(err);
            //res.json({code:9999,msg:'操作失败，请刷新后重试，或者联系客服'});
        });
    },
    give_coupon:function(req,res,next){
        var uid = req.AV.user.id ;
        serviceM.give_coupon(uid).then(function(data){
            res.json(data);
            /*if(data.code === 0){
             res.json({code:0,msg:'操作成功，请前往商城个人中心查看'});
             }else{
             res.json({code:-1,msg:'操作失败，请刷新后重试，或者联系客服'});
             }*/
        }).catch(function(err){
            res.json(err);
            //res.json({code:9999,msg:'操作失败，请刷新后重试，或者联系客服'});
        });
    },
    give_coupon2:function(req,res,next){

    }

};